/**
 * @file Traversal.cc
 * @author snow-tyan (zziywang@163.com)
 * @brief {Life is too short to learn cpp.}
 * @version 0.1
 * @date 2021-11-12
 * 
 * @copyright Copyright (c) 2021
 * 
 * 二叉树的遍历 递归+非递归
 * 144.前序
 * 94.中序
 * 145.后序
 * 102.层序
 */

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int val = 0, TreeNode *left = nullptr, TreeNode *right = nullptr)
        : val(val), left(left), right(right) {}
};

class Solution
{
public:
    // 递归 略吧，没什么可写的
#if 0
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root, res);
        return res;
    }
    void traversal(TreeNode *root, vector<int> &res){
        if (!root){
            return;
        }
        res.push_back(root->val);
        traversal(root->left, res);
        traversal(root->right, res);
    }
#endif

    // 非递归
    vector<int> preorderTraversal(TreeNode *root)
    {
        if (!root) {
            return {};
        }
        vector<int> res;
        stack<TreeNode *> stk;
        stk.push(root);
        while (!stk.empty()) {
            TreeNode *node = stk.top();
            res.push_back(node->val);
            stk.pop();
            if (node->right) {
                stk.push(node->right);
            }
            if (node->left) {
                stk.push(node->left);
            }
        }
        return res;
    }
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> res;
        stack<TreeNode *> stk;
        TreeNode *cur = root;
        while (cur || !stk.empty()) {
            while (cur) {
                stk.push(cur);
                cur = cur->left;
            }
            TreeNode *node = stk.top();
            stk.pop();
            res.push_back(node->val);
            cur = node->right;
        }
        return res;
    }
    vector<int> postorderTraversal(TreeNode *root)
    {
        if (!root) {
            return {};
        }
        vector<int> res;
        stack<TreeNode *> stk;
        stk.push(root);
        while (!stk.empty()) {
            TreeNode *node = stk.top();
            res.push_back(node->val);
            stk.pop();
            if (node->left) {
                stk.push(node->left);
            }
            if (node->right) {
                stk.push(node->right);
            }
        }
        std::reverse(res.begin(), res.end());
        return res;
    }
    // 层序
    vector<vector<int>> levelOrder(TreeNode *root)
    {
        if (!root) {
            return {};
        }
        vector<vector<int>> res;
        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            int n = q.size();
            vector<int> layer;
            for (int i = 0; i < n; ++i) { // for循环遍历一层节点
                TreeNode *node = q.front();
                q.pop();
                if (node->left) {
                    q.push(node->left);
                }
                if (node->right) {
                    q.push(node->right);
                }
                layer.push_back(node->val);
            }
            res.emplace_back(layer);
        }
        return res;
    }
};